package com.happy.manage.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.happy.common.annotation.Log;
import com.happy.common.core.controller.BaseController;
import com.happy.common.core.domain.AjaxResult;
import com.happy.common.enums.BusinessType;
import com.happy.manage.domain.Microposts;
import com.happy.manage.domain.MicropostsWithUser;
import com.happy.manage.service.IMicropostsService;
import com.happy.common.utils.poi.ExcelUtil;
import com.happy.common.core.page.TableDataInfo;

/**
 * 微贴子Controller
 * 
 * @author ruoyi
 * @date 2024-12-27
 */
@RestController
@RequestMapping("/manage/microposts")
public class MicropostsController extends BaseController
{
    @Autowired
    private IMicropostsService micropostsService;

    /**
     * 查询微贴子列表
     */
    @PreAuthorize("@ss.hasPermi('manage:microposts:list')")
    @GetMapping("/list")
    public TableDataInfo list(MicropostsWithUser microposts)
    {
        startPage();
        List<MicropostsWithUser> list = micropostsService.selectMicropostsWithUserList(microposts);
        return getDataTable(list);
    }

    /**
     * 导出微贴子列表
     */
    @PreAuthorize("@ss.hasPermi('manage:microposts:export')")
    @Log(title = "微贴子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MicropostsWithUser microposts)
    {
        List<MicropostsWithUser> list = micropostsService.selectMicropostsWithUserList(microposts);
        ExcelUtil<MicropostsWithUser> util = new ExcelUtil<MicropostsWithUser>(MicropostsWithUser.class);
        util.exportExcel(response, list, "微贴子数据");
    }

    /**
     * 获取微贴子详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:microposts:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return success(micropostsService.selectMicropostsByPostId(postId));
    }

    /**
     * 新增微贴子
     */
    @PreAuthorize("@ss.hasPermi('manage:microposts:add')")
    @Log(title = "微贴子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Microposts microposts)
    {
        return toAjax(micropostsService.insertMicroposts(microposts));
    }

    /**
     * 修改微贴子
     */
    @PreAuthorize("@ss.hasPermi('manage:microposts:edit')")
    @Log(title = "微贴子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Microposts microposts)
    {
        return toAjax(micropostsService.updateMicroposts(microposts));
    }

    /**
     * 删除微贴子
     */
    @PreAuthorize("@ss.hasPermi('manage:microposts:remove')")
    @Log(title = "微贴子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(micropostsService.deleteMicropostsByPostIds(postIds));
    }
}
